﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;

namespace CRM.Web.AppointmentReminderManage
{
    public partial class Modify : Common.PageBase
    {

        T_APPOINTMENTREMINDER_BLL bll = new T_APPOINTMENTREMINDER_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            txtAPPOINTMENTTIME.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',errDealMode:'1'})");

        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                pageDataInit();
            }

        }

        /// <summary>
        /// Pages the data init.
        /// </summary>
        private void pageDataInit()
        {

            switch ((AppointmentReminderType)Enum.Parse(typeof(AppointmentReminderType), PageHelper.GetQueryString(this.Page, "APPOINTMENTTYPE")))
            {
                case AppointmentReminderType.报修工单:
                    {
                        lblTitle.Text = "报修工单预约";
                        Header.Title = lblTitle.Text;
                    }
                    break;
                case AppointmentReminderType.投诉工单:
                    {
                        lblTitle.Text = "投诉工单预约";
                        Header.Title = lblTitle.Text;
                    }
                    break;
            }


        }



        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(PageHelper.GetQueryString(this.Page, "APPOINTMENTTYPE")))
            {
                return;
            }
            T_APPOINTMENTREMINDER_Entity entity = new T_APPOINTMENTREMINDER_Entity();

            PageHelper.GetControlValue<T_APPOINTMENTREMINDER_Entity>(this.Page, ref entity);
            if (entity.APPOINTMENTTIME.HasValue)
            {
                entity.REMINDERTIME = entity.APPOINTMENTTIME.Value.AddMinutes(-10);
            }
            else
            {
                entity.REMINDERTIME = null;
            }

            entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
            entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
            entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
            entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
            entity.CREATETIME = DateTime.Now;

            entity.APPOINTMENTKEY = PageHelper.GetQueryString(this.Page, "APPOINTMENTKEY");
            entity.APPOINTMENTTYPE = PageHelper.GetQueryString(this.Page, "APPOINTMENTTYPE");

            entity.CASEID = PageHelper.GetQueryString(this.Page, "CASEID");
            entity.CUSTOMERID = PageHelper.GetQueryString(this.Page, "CUSTOMERID");
            entity.TASKID = PageHelper.GetQueryString(this.Page, "TASKID");

            switch ((AppointmentReminderType)Enum.Parse(typeof(AppointmentReminderType), entity.APPOINTMENTTYPE))
            {
                case AppointmentReminderType.报修工单:
                    {
                        entity.APPOINTMENTURL = String.Format("../RepairCaseManage/Modify.aspx?CASEID={0}", entity.CASEID);
                        entity.APPOINTMENTSUBJECT = String.Format("报修工单预约");
                    }
                    break;
                case AppointmentReminderType.投诉工单:
                    {
                        entity.APPOINTMENTURL = String.Format("../ComplaintCaseManage/Modify.aspx?CASEID={0}", entity.CASEID);
                        entity.APPOINTMENTSUBJECT = String.Format("投诉工单预约");
                    }
                    break;
            }
            
            entity.STATUS = (int)AppointmentReminderStatus.待提醒;
            entity.TIMEOUT = 24;



            try
            {
                long rel = bll.Insert(entity);
                if (rel > 0)
                {
                    PageHelper.ShowMessage("预约添加成功！", true, false);
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, "预约添加失改！");
                }

            }
            catch (Exception ex)
            {

                PageHelper.ShowMessage(this.Page, "预约添加失改！" + ex.Message);
            }
            

        }

    }
}
